import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author: gao
 * @create-date: 2025/3/25 21:05
 */

public class Solution {
    public int findMaxLength(int[] nums){
        Map<Integer,Integer> hash = new HashMap<>();
        hash.put(0,-1);

        int sum = 0,ret = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += (nums[i] == 0 ? -1 : 1);//计算当前位置的前缀和
            if(hash.containsKey(sum)) ret = Math.max(ret,i-hash.get(sum));
            else hash.put(sum,i);
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int len = in.nextInt();
            int[] nums = new int[len];
            for (int i = 0; i < len; i++) {
                nums[i] = in.nextInt();
            }
            Solution solution = new Solution();
            System.out.println(solution.findMaxLength(nums));
        }
    }
}
